﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model.Dto;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services.Db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Services.Impl
{
    public class OrganizationService : BaseEFService<Organization>, IOrganizationService
    {
        Context db;

        public OrganizationService(Context context) : base(context)
        {
            db = context;
        }
        public async Task<int> Remove(Organization org)
        {
            _ = db.Organization.Remove(org);
            return await db.SaveChangesAsync();
        }

        public async Task<List<OrganizationDto>> QueryPageAsync(OrganizationDto dto)
        {
            var where = ExtLinq.True<OrganizationDto>();
            if (!string.IsNullOrEmpty(dto.Key))
            {
                where = where.And(o => o.CNName.Contains(dto.Key) || o.Code.Contains(dto.Key) || o.CNAbbr.Contains(dto.Key));
            }
            var linq = db.Organization
                .Select(a => new OrganizationDto
                {
                    Id = a.Id,
                    Code = a.Code,
                    CNName = a.CNName,
                    CNAbbr = a.CNAbbr,
                    ENName = a.ENName,
                    ENAbbr = a.ENAbbr,
                    Email = a.Email,
                    CreateDate = a.CreateDate,
                    CreateUserId = a.CreateUserId,
                    UpdateDate = a.UpdateDate,
                    UpdateUserId = a.UpdateUserId,
                })
                .Where(where);
            var result = new List<OrganizationDto>();
            if(dto.Pagination != null)
            {
                dto.Pagination.DataCount = linq.Count();
                result = await linq
                    .Skip((dto.Pagination.Page - 1) * dto.Pagination.PageSize)
                    .Take(dto.Pagination.PageSize)
                    .ToListAsync();
            }
            else
            {
                result = await linq.ToListAsync();
            }
            return result;
        }

        public OrganizationDto QuerySingle(Expression<Func<OrganizationDto, bool>> where)
        {
            var data = db.Organization
                .Select(a => new OrganizationDto
                {
                    Id = a.Id,
                    Code = a.Code,
                    CNName = a.CNName,
                    CNAbbr = a.CNAbbr,
                    ENName = a.ENName,
                    ENAbbr = a.ENAbbr,
                    Email = a.Email,
                    CreateDate = a.CreateDate,
                    CreateUserId = a.CreateUserId,
                    UpdateDate = a.UpdateDate,
                    UpdateUserId = a.UpdateUserId,
                })
                .Where(where)
                .Single();
            return data;
        }

        public Task<OrganizationDto> QuerySingleAsync(Expression<Func<OrganizationDto, bool>> where)
        {
            return db.Organization
                .Select(a => new OrganizationDto
                {
                    Id = a.Id,
                    Code = a.Code,
                    CNName = a.CNName,
                    CNAbbr = a.CNAbbr,
                    ENName = a.ENName,
                    ENAbbr = a.ENAbbr,
                    Email = a.Email,
                    CreateDate = a.CreateDate,
                    CreateUserId = a.CreateUserId,
                    UpdateDate = a.UpdateDate,
                    UpdateUserId = a.UpdateUserId,
                })
                .Where(where).SingleAsync();
        }
        public async Task<int> EditAsync(OrganizationDto dto)
        {
            var org = await db.Organization.Where(o=>o.Id == dto.Id).SingleAsync();
            if(org is null)
            {
                return 0;
            }
            org.CNName = dto.CNName;
            org.CNAbbr = dto.CNAbbr;
            org.ENName = dto.ENName;
            org.ENAbbr = dto.ENAbbr;
            org.UpdateDate = DateTime.Now;
            return await db.SaveChangesAsync();
        }
    }
}
